//
//  TaskImageViewController.swift
//  willdo
//
//  Created by zz on 4/10/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit
import Nuke

class TaskImageViewController: UICollectionViewController {
    var photo:[URL]!
    
    var manager = Nuke.Manager.shared
    var itemsPerRow = 4
    
    let bigImgView = UIView()
    let bigImg = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
       // self.automaticallyAdjustsScrollViewInsets = false
       // self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        setupBigImg()
        photo = demoPhotosURLs
        collectionView?.backgroundColor = UIColor.white
        collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "imgCell")
    }

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        updateItemSize()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        updateItemSize()
    }
    
    func updateItemSize() {
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumLineSpacing = 2.0
        layout.minimumInteritemSpacing = 2.0
        let side = (Double(view.bounds.size.width) - Double(itemsPerRow - 1) * 2.0) / Double(itemsPerRow)
        layout.itemSize = CGSize(width: side, height: side)
    }

    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photo.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imgCell", for: indexPath)
        cell.backgroundColor = BACKGROUND_GRAY
        let imgView = imageViewForCell(cell)
        let hideGes = UITapGestureRecognizer(target: self, action: #selector(showImg(img:)))
        let request = Request(url: photo[indexPath.row])
        manager.loadImage(with: request, into: imgView)
        return cell
    }

    func imageViewForCell(_ cell: UICollectionViewCell) -> UIImageView {
        var imageView: UIImageView! = cell.viewWithTag(15) as? UIImageView
        if imageView == nil {
            imageView = UIImageView(frame: cell.bounds)
            imageView.autoresizingMask =  [.flexibleWidth, .flexibleHeight]
            imageView.tag = 15
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            cell.addSubview(imageView!)
        }
        return imageView!
    }

    func setupBigImg(){
        view.addSubview(bigImgView)
        bigImgView.backgroundColor = UIColor.black
        bigImgView.isHidden = true
        bigImgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        bigImgView.addSubview(bigImg)
        bigImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(bigImgView.snp.width)
            make.center.equalToSuperview()
        }
        bigImg.isUserInteractionEnabled = true
        let hideGes = UITapGestureRecognizer(target: self, action: #selector(hideImg))
        bigImg.addGestureRecognizer(hideGes)
    }
    
    @objc func showImg(img:ImageView){
        let imgV = img.image
        bigImgView.isHidden = false
        view.bringSubview(toFront: bigImgView)
        bigImg.image = imgV
    }
    @objc func hideImg(){
        bigImgView.isHidden = true
        bigImg.image = nil
    }
}

class imgCell: UICollectionViewCell{
    let imgView = UIImageView()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(imgView)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

private let host = "https://cloud.githubusercontent.com/assets"
let demoPhotosURLs = [
    URL(string: "\(host)/1567433/9781817/ecb16e82-57a0-11e5-9b43-6b4f52659997.jpg")!,
    URL(string: "\(host)/1567433/9781832/0719dd5e-57a1-11e5-9324-9764de25ed47.jpg")!,
    URL(string: "\(host)/1567433/9782132/548a3b9a-57a3-11e5-8228-8ee523e7809e.jpg")!
]
